package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication.db.Type;

import java.util.List;

public class CategoryAdapter extends ArrayAdapter<Type> {
    private List<Type> mData;
    //     当前选中元素
    private int clickItem = 0;
    public CategoryAdapter(@NonNull Context context, int resource, List<Type> objects) {
        super(context, resource,objects);
        mData = objects;
    }

    public void  selectItem(int position){
        clickItem = position;
    }

    public int getClickItem(){
        return clickItem;
    }

    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Type type = getItem(position);//得到当前项的实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.category_item, parent, false);
        //分别获取 image view 和 textview 的实例
        ImageView itemImage = view.findViewById(R.id.category_image);
        TextView itemTitle = view.findViewById(R.id.category_message);
        Button itemButton =view.findViewById(R.id.category_delete_btn);

        //         设置圆形背景
        itemImage.setBackgroundResource(R.drawable.round_background);
        // 设置要显示的图片和文字
        itemImage.setImageResource(type.getIcon());
        itemTitle.setText(type.getType());

        itemButton.setOnClickListener(view1 -> {
            mData.remove(position);
            notifyDataSetChanged();
        });

        return view;
    }
}
